import '../models/word.dart';
import '../models/quiz_question.dart';
import '../../core/constants.dart';
import '../../data/master_vocabulary.dart';

class WordService {
  // Mock data for demonstration
  static final List<Word> _mockWords = [
    const Word(
      id: '1',
      esperanto: 'hundo',
      english: 'dog',
      pronunciation: 'HUN-do',
      definition: 'A domesticated carnivorous mammal',
      examples: ['La hundo estas fidela amiko', 'Mi havas belan hundon'],
      category: WordCategories.ANIMALS,
      audioUrl: '',
    ),
    const Word(
      id: '2',
      esperanto: 'kato',
      english: 'cat',
      pronunciation: 'KA-to',
      definition: 'A small domesticated carnivorous mammal',
      examples: ['La kato dormas sur la lito', 'Nia kato estas nigra'],
      category: WordCategories.ANIMALS,
      audioUrl: '',
    ),
    const Word(
      id: '3',
      esperanto: 'rozo',
      english: 'rose',
      pronunciation: 'RO-zo',
      definition: 'A woody perennial flowering plant',
      examples: ['La rozo odoras bone', 'Mi donis al ŝi ruĝan rozon'],
      category: WordCategories.PLANTS,
      audioUrl: '',
    ),
    const Word(
      id: '4',
      esperanto: 'pano',
      english: 'bread',
      pronunciation: 'PA-no',
      definition: 'A staple food prepared from flour and water',
      examples: ['Mi manĝas panon je matenmanĝo', 'La pano estas freŝa'],
      category: WordCategories.DAILY_LIFE,
      audioUrl: '',
    ),
    const Word(
      id: '5',
      esperanto: 'saluton',
      english: 'hello',
      pronunciation: 'sa-LU-ton',
      definition: 'A greeting used when meeting someone',
      examples: ['Saluton! Kiel vi fartas?', 'Mi diris saluton al la najbaro'],
      category: WordCategories.COMMON_PHRASES,
      audioUrl: '',
    ),
  ];

  static final List<QuizQuestion> _mockQuestions = [
    const QuizQuestion(
      id: '1',
      question: 'What does "hundo" mean in English?',
      options: ['cat', 'dog', 'bird', 'fish'],
      correctAnswerIndex: 1,
      explanation: '"Hundo" is the Esperanto word for dog.',
      category: WordCategories.ANIMALS,
      difficulty: 'easy',
    ),
    const QuizQuestion(
      id: '2',
      question: 'How do you say "hello" in Esperanto?',
      options: ['saluton', 'dankon', 'bonvolu', 'pardonu'],
      correctAnswerIndex: 0,
      explanation: '"Saluton" is the standard greeting in Esperanto.',
      category: WordCategories.COMMON_PHRASES,
      difficulty: 'easy',
    ),
  ];

  Future<List<Word>> getWordsByCategory(String category) async {
    await Future.delayed(const Duration(milliseconds: 500)); // Simulate API call
    final allWords = MasterVocabulary.allVocabulary;
    
    if (category == WordCategories.RANDOM) {
      final shuffled = List<Word>.from(allWords)..shuffle();
      return shuffled.take(50).toList();
    }
    return allWords.where((word) => word.category == category).toList();
  }

  Future<List<Word>> searchWords(String query) async {
    await Future.delayed(const Duration(milliseconds: 300)); // Simulate API call
    final allWords = MasterVocabulary.allVocabulary;
    return allWords.where((word) =>
      word.esperanto.toLowerCase().contains(query.toLowerCase()) ||
      word.english.toLowerCase().contains(query.toLowerCase()) ||
      word.definition.toLowerCase().contains(query.toLowerCase())
    ).toList();
  }

  Future<Word?> getWordById(String id) async {
    await Future.delayed(const Duration(milliseconds: 200)); // Simulate API call
    try {
      // 首先在实际词汇数据库中查找
      final word = MasterVocabulary.allVocabulary.firstWhere((word) => word.id == id);
      return word;
          } catch (e) {
        // 如果在实际数据中找不到，再在mock数据中查找（向后兼容）
        try {
          return _mockWords.firstWhere((word) => word.id == id);
        } catch (e2) {
          return null;
        }
      }
  }

  Future<List<QuizQuestion>> getQuizQuestions(String category, int count) async {
    await Future.delayed(const Duration(milliseconds: 500)); // Simulate API call
    List<QuizQuestion> questions = _mockQuestions;
    if (category != WordCategories.RANDOM) {
      questions = questions.where((q) => q.category == category).toList();
    }
    questions.shuffle();
    return questions.take(count).toList();
  }

  Future<List<Word>> getAllWords() async {
    await Future.delayed(const Duration(milliseconds: 300)); // Simulate API call
    return List<Word>.from(MasterVocabulary.allVocabulary);
  }
} 